// pages/index/index.js
const http = require('../../utils/http.js')
const config = require('../../config.js')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userPic: '',  //头像
    qrCode: '', //二维码
    isVideo: false, //是否有视频
    getArticData: '', //  获取文章url
    flgId: null,  //标志是从用户分享的连接进入还是文章列表进入
    // articId: null,  //标志是从文章列表进入
    imgList: [],  //图片地址列表
    sudoku: [], //九宫格图片列表
    article: {},  //文章内容
    user: {},   //用户信息
    publishDate: '',  //文章发布时间
    thumbList: [],  //点赞人数
    video: '',  //视频地址
    thumbUp: '', //是否点赞
    thumbUps: '', //点赞人数
    articId: '', //文章ID
    flgName: '',    //表示是从分享进入还是列表进入
    parentId: 1, //分享人ID,默认为1
    shareNick: '和之初',  //分享人昵称
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    var that = this
    if (options.hasOwnProperty('parentId')) {
      that.setData({
        parentId: options.parentId
      })
    }
    wx.setStorageSync("parentId", that.data.parentId)
    //判断路径传参是否有指定参数,articId为文章列表进入，shareId为分享链接进入,flgId为文章ID或者分享ID
    if (options.hasOwnProperty('articId')) {
      that.setData({
        getArticData: config.formListArticle,
        flgId: options.articId,
        flgName: "article"
      })
    }
    if (options.hasOwnProperty('shareId')) {
      wx.setStorageSync("parentId", options.shareId)
      that.setData({
        getArticData: config.formUserArticle,
        flgId: options.shareId,
        flgName: "share"
      })
    } 
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    that.getArticlesData()
    console.log(that.data.getArticData)
  },

  /**
   * 获取页面渲染数据
   */
  getArticlesData: function() {
    var that = this
    var data = {
      
    }
    var url = that.data.getArticData + '/' + that.data.flgId  //将onLoad获取的参数进行拼接,组成url
    http.httpReq(url, "GET", data, function (res) {
      console.log(res)
      var userPic = res.data.user.avatar
      var qrCode = app.handleImg(res.data.user.qrcode)
      var isVideo = true  
      if (res.data.article.video.length == 0 ) {
        isVideo = false //如果没有视频，则不显示video组件
      }else{
        that.setData({
          video: res.data.article.video, 
        }) 
      }
      var thumbList = res.data.list
      that.setData({
        userPic,
        isVideo,
        qrCode,
        article: res.data.article,
        user: res.data.user,
        thumbList: thumbList,
        thumbUp: res.data.thumbUp,
        thumbUps: res.data.article.thumbCount,
        articId: res.data.article.id
      })
      var imgList = app.imgArr(that.data.article.imgs)
      that.setData({
        imgList: imgList,
        publishDate: that.data.article.publishDate.slice(0, 10) //文章发布时间，将时分秒去除，保留年月日
      })
      that.handleImgList()
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    var that = this
    if (that.data.flgName == "article") {
      var article = "/pages/index/index?articId="
    }
    if (that.data.flgName == "share") {
      var article = "/pages/index/index?shareId="
    }
    return {
      title: that.data.article.title,
      desc: '来自' + that.data.shareNick + '的分享',
      path: article + that.data.flgId + "&parentId=" + that.data.parentId, // 路径，传递参数到指定页面。
    }
  },
  /**
   * 拨打电话
   */
  makePhoneCallTap: function() {
    wx.makePhoneCall({
      phoneNumber: this.data.user.phone 
    })
  },
  /**
   * 修改成我的信息
   */
  userInfoTap: function() {
    wx.navigateTo({
      url: '../user/index?articId=' + this.data.articId ,
    })
  },
  /**
   * 图片九宫格展示,超出图片隐藏，默认在大图展示
   */
  handleImgList: function() {
    var that = this
    var sudoku = []
    for (var i = 0; i < that.data.imgList.length; i++) {
      if(i >= 9) {
        return
      }
      sudoku.push(that.data.imgList[i])
    }
    that.setData({
      sudoku
    })
  },
  /**
   * 点击查看大图
   */
  dispBigTap: function(e) {
    var that = this
    var url = e.currentTarget.dataset.url
    var imgList = that.data.imgList
    wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls: imgList // 需要预览的图片http链接列表
    })
  },
  /**
   * 文章点赞
   */
  thumbTap: function() {
    var that = this
    that.setData({
      thumbUp: !that.data.thumbUp
    })
    var data = {
      articleId: that.data.article.id
    }
    http.httpReq(config.articleThumb, "POST", data, function(res){
      var thumbList = res.data.thumbUpList
      that.setData({
        thumbList,
        thumbUps: res.data.thumbUps
      })
    })
  },
  /**
   * 去首页
   */
  goHomeTap: function(){
    wx.reLaunch({
      url: "/pages/article/index"
    })
  }
})